package com.open.party.cloud.view.examine

import android.os.Bundle
import androidx.core.content.ContextCompat
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.sinothk.hussars.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.ExamineClassCourseEntity
import com.open.party.cloud.model.ExamineClassVO
import com.open.party.cloud.view.examine.adapter.ExamineClassCourseListAdapter
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.dialog.bottomDialog.dateDialog.TimePickerDialog
import com.sinothk.dialog.bottomDialog.dateDialog.data.Type
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.activity_load_recycle_view_load_tip_app_bg_class_course_page.*
import kotlinx.android.synthetic.main.activity_load_recycle_view_load_tip_app_bg_class_course_page.loadingRecyclerView
import kotlinx.android.synthetic.main.activity_load_recycle_view_load_tip_app_bg_class_course_page.loadingTipView
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.text.SimpleDateFormat
import java.util.*

class ExamineClassCoursePageActivity : AppEventBusRecycleViewTitleBaseActivity<String>(), OnSuperListener<ExamineClassCourseEntity> {

    var viewModel: ExamineViewModel? = null
    var classId: String? = null
    var selectedDate: Date? = null
    private var adapter: ExamineClassCourseListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.activity_load_recycle_view_load_tip_app_bg_class_course_page

    private fun showDateSelected() {
        val oneYear: Long = 365 * 24 * 60 * 60 * 1000L
        val mDialogYearMonth: TimePickerDialog = TimePickerDialog.Builder()
            .setTitleStringId("选择日期")
            .setType(Type.YEAR_MONTH_DAY)
            .setMinMillseconds(System.currentTimeMillis() - (1 * oneYear))
            .setMaxMillseconds(System.currentTimeMillis() + (2 * oneYear))
            .setThemeColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .setCallBack { _, millSeconds ->
                val dateObj = Date(millSeconds)
                val selectedMonth: String = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault()).format(dateObj)
                dateTv.text = selectedMonth
                selectedDate = dateObj
                refreshData()
            }
            .build()

        mDialogYearMonth.show(supportFragmentManager, "year_month")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        classId = intent.getStringExtra("classId")
        setViewTitle("课程表")

        viewModel = ExamineViewModel()

        selectedDate = null
        dateTv.text = "所有课程"//XUtils.date().getDateStrByDate(Date(), "yyyy年MM月dd日")
        initView()

        dateItem.setOnClickListener {
            showDateSelected()
        }
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = ExamineClassCourseListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<String>) {
        val pageReq = PageReq<ExamineClassVO>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = 20
        val vo = ExamineClassVO()
        vo.classId = classId
        if (selectedDate != null) {
            vo.courseDate = XUtils.date().getDateStrByDate(selectedDate, "yyyy-MM-dd")
        }
        pageReq.data = vo

        viewModel!!.getSchoolCoursePage(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<ExamineClassCourseEntity>>) {
        if (result.eventType != "getSchoolCoursePage") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<ExamineClassCourseEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: ExamineClassCourseEntity, flag: String?) {
//        XUtils.toast().show("评价")
    }
}